<?php
/**
 * Created by PhpStorm.
 * User: lijie
 * Date: 2018/10/31
 * Time: 16:10
 */
namespace app\api\model;
use think\Model;
use think\Db;
class Orderupto extends Model{

    protected $table = "orange_orderuptouser";
    protected $autoWriteTimestamp = true;
    protected $createTime = "ctime";
    protected $updateTime = "utime";
    protected $dateFormat;
    /*
    * Notes: 添加充值订单
    * Create Date: 2018/10/5
    * Create Time: 下午4:36
    * Create by :张福来
    * param :
    */
    public function addOrder($addarray){
        $result = $this
                ->save($addarray);
        return $result;

    }
    /*
    * Notes:
    * Create Date: 2018/10/5
    * Create Time: 下午5:35
    * Create by :张福来
    * param :
    */
    public function selectOrder($pagenum,$listNum,$w){
    $result["list"] = $this
            ->where($w)
            ->page($pagenum,$listNum)
            ->select();
    $result["total"] = $this
                    ->where($w)
                    ->count("id");
    return $result;
    }
    /*
    * Notes:
    * Create Date: 2018/10/5
    * Create Time: 下午5:41
    * Create by :张福来
    * param :
    */
    public function updateOreder($w){
        Db::startTrans();
        $order = $this->where($w)->find();
        $allprice = floatval($order["topup"]) + floatval($order["backmoney"]);
        $addarray["type"] = 1;
        $addarray["u_id"] = $order["u_id"];
        $addarray["title"] = "一卡通充值";
        $addarray["content"] = "您为一卡通充值".$order["topup"]."元";
        $addarray["status"] = 0;
        $addarray["utime"] = time();
        $addarray["ctime"] = time();
        $addarray["status"] = 0;
        $bill["u_id"] = $order["u_id"];
        $bill["price"] = $order["topup"];
        $bill["content"] = "为一卡通充值".$order["topup"]."元";
        $bill["cardnumber"] = $order["ordernumber"];
        $bill["utime"] = time();
        $bill["ctime"] = time();
        $bill["status"] = 0;
        $result = Db::name("orderupto")
                ->where($w)
                ->update(['pay_time'=>time(),'status' =>1]);
        $result1 = Db::name("onecard")->where(["u_id"=>$order["u_id"]])->setInc("wallet",$allprice);
        $result2 = Db::name("uptorecord")->insert($bill);
        $result3 = Db::name("message")->insert($addarray);
        file_put_contents("t.txt",$result.$result1.$result2.$result3);
        if($result1 && $result2 && $result3 && $result){
            Db::commit();
            return true;
        }else{
            Db::rollback();
            return false;
        }
    }
}